package com.homework.HomeAcount.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.homework.HomeAcount.entity.Role;
import com.homework.HomeAcount.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/getAllRoles")
	public @ResponseBody List<Role> getAllRoles() {
		return roleService.getAllRoles();
	}
	
	@RequestMapping("/roleAllocated")
	public @ResponseBody Map<String, Object> roleAllocated(String userId,String roleIds) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		String[] roleArr = roleIds.split(",");
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for(int i = 0; i < roleArr.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("roleId", roleArr[i]);
			list.add(map);
		}
 		try {
			roleService.roleAllocated(userId, list);
			reMap.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("flag", false);
		}
		return reMap;
	}
}
